package com.jiantong.work.impl.manager;

import com.google.common.collect.Maps;
import com.jiantong.common.util.UserUtil;
import com.jiantong.user.ext.LoginUser;
import com.jiantong.user.impl.dao.BreedFactoryDao;
import com.jiantong.user.impl.dao.BreedFactoryStableDao;
import com.jiantong.user.model.BreedFactory;
import com.jiantong.user.model.BreedFactoryStable;
import com.jiantong.work.enums.*;
import com.jiantong.work.impl.dao.AnimalMarkDao;
import com.jiantong.work.impl.dao.ProductionReportDao;
import com.jiantong.work.impl.dao.QuarantineCenterHarmlessDao;
import com.jiantong.work.impl.dao.ReportHandleResumeDao;
import com.jiantong.work.model.ProductionReport;
import com.jiantong.work.model.QuarantineCenterHarmless;
import com.jiantong.work.model.ReportHandleResume;
import com.jiantong.work.util.Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * Created by niufei on 2017/3/30.
 *
 */
@Component
public class QuarantineCenterHarmlessManager {

    private final QuarantineCenterHarmlessDao quarantineCenterHarmlessDao;

    private final ReportHandleResumeDao reportHandleResumeDao;

    private final BreedFactoryDao breedFactoryDao;

    private final ProductionReportManager productionReportManager;

    private final ProductionReportDao productionReportDao;

    private final BreedFactoryStableDao breedFactoryStableDao;

    private final AnimalMarkDao animalMarkDao;


    @Autowired
    public QuarantineCenterHarmlessManager(QuarantineCenterHarmlessDao quarantineCenterHarmlessDao,
                                           ReportHandleResumeDao reportHandleResumeDao,
                                           BreedFactoryDao breedFactoryDao,
                                           ProductionReportManager productionReportManager,
                                           ProductionReportDao productionReportDao,
                                           BreedFactoryStableDao breedFactoryStableDao,
                                           AnimalMarkDao animalMarkDao) {
        this.quarantineCenterHarmlessDao = quarantineCenterHarmlessDao;
        this.reportHandleResumeDao = reportHandleResumeDao;
        this.breedFactoryDao = breedFactoryDao;
        this.productionReportManager = productionReportManager;
        this.productionReportDao = productionReportDao;
        this.breedFactoryStableDao = breedFactoryStableDao;
        this.animalMarkDao = animalMarkDao;
    }

    /**
     * 新建防疫中心--无害化 单据
     * @param quarantineCenterHarmless 防疫中心--无害化 单据
     * @param toStatus 到状态
     * @return Map<String , Object>
     */
    @Transactional
    public  Map<String , Object> createQuarantineCenterHarmless(QuarantineCenterHarmless quarantineCenterHarmless,Integer toStatus){
        Integer fromStatus = quarantineCenterHarmless.getStatus();
        ReportHandleResume reportHandleResume;
        Map<String , Object> map = Maps.newHashMap();

        LoginUser loginUser = UserUtil.getCurrentUser();
        String prefix = "FW";
        prefix = Utils.createPrefix(prefix,loginUser);//单据编号前缀的开头字母 和 当前用户
        String number = Utils.create(prefix,quarantineCenterHarmlessDao.findCurrentMaxNumber(prefix));//要在表中拿到当前最大的Number号
        quarantineCenterHarmless.setNumber(number);
        quarantineCenterHarmless.setDeleteFlag(DocumentsDeleteFlag.NORMAL.value());
        if(toStatus != null && !fromStatus.equals(toStatus)){
            quarantineCenterHarmless.setStatus(toStatus);
            quarantineCenterHarmlessDao.create(quarantineCenterHarmless);
            map.put("quarantineCenterHarmlessId",quarantineCenterHarmless.getId());

            reportHandleResume = new ReportHandleResume();
            reportHandleResume.setReportId(quarantineCenterHarmless.getId());
            reportHandleResume.setReportNumber(quarantineCenterHarmless.getNumber());
            reportHandleResume.setType(DocumentsType.QUARANTINE_CENTER_HARMLESS.value());
            reportHandleResume.setFromStatus(fromStatus);
            reportHandleResume.setToStatus(toStatus);
            reportHandleResume.setUserId(loginUser.getId());
            reportHandleResume.setUserName(loginUser.getName());
            reportHandleResumeDao.create(reportHandleResume);
            map.put("reportHandleResumeId",reportHandleResume.getId());

            if(toStatus.equals(QuarantineCenterHarmlessStatus.FINISHED.value())){
                ProductionReport productionReport = buildProductionReport(quarantineCenterHarmless);
                productionReportDao.create(productionReport);
//                productionReportManager.createProductionReport(buildProductionReport(quarantineCenterHarmless),1);
                BreedFactory breedFactory = new BreedFactory();
                breedFactory.setId(productionReport.getBreedFactoryId());
                breedFactory.setStockQuantity(productionReport.getChangeQuantity());
                breedFactoryDao.updateFromSC(breedFactory);
                if(productionReport.getStableId() != null) {
                    BreedFactoryStable breedFactoryStable = new BreedFactoryStable();
                    breedFactoryStable.setId(productionReport.getStableId());
                    breedFactoryStable.setQuantity(productionReport.getChangeQuantity());
                    breedFactoryStableDao.updateFromSC(breedFactoryStable);
                    List<String> earNumbers = quarantineCenterHarmless.getEarNumbers();
                    if (earNumbers != null && !earNumbers.isEmpty()) {
                        Map<String, Object> map1 = Maps.newHashMap();
                        map1.put("markNumbers", earNumbers);
                        map1.put("status", AnimalMarkStatus.USED.value());
                        map1.put("outedAt", new Date());
                        animalMarkDao.updateMarks(map1);
                    }
                }
            }

        }else {
            quarantineCenterHarmlessDao.create(quarantineCenterHarmless);
            map.put("quarantineCenterHarmlessId",quarantineCenterHarmless.getId());
        }
        return map;
    }

    /**
     * 更新防疫中心--无害化 单据
     * @param quarantineCenterHarmless 防疫中心--无害化 单据
     * @param toStatus 到状态
     * @return Map<String , Object>
     */
    @Transactional
    public Map<String , Object> updateQuarantineCenterHarmless(QuarantineCenterHarmless quarantineCenterHarmless,Integer toStatus){
        Integer fromStatus = quarantineCenterHarmless.getStatus();
        ReportHandleResume reportHandleResume;
        Boolean flagQuarantineCenterHarmless;
        Map<String , Object> map = Maps.newHashMap();
        if(toStatus != null && !fromStatus.equals(toStatus)){
            quarantineCenterHarmless.setStatus(toStatus);
            flagQuarantineCenterHarmless = quarantineCenterHarmlessDao.update(quarantineCenterHarmless);
            map.put("flagQuarantineCenterHarmless",flagQuarantineCenterHarmless);

            reportHandleResume = new ReportHandleResume();
            reportHandleResume.setReportId(quarantineCenterHarmless.getId());
            reportHandleResume.setReportNumber(quarantineCenterHarmless.getNumber());
            reportHandleResume.setType(DocumentsType.QUARANTINE_CENTER_HARMLESS.value());
            reportHandleResume.setFromStatus(fromStatus);
            reportHandleResume.setToStatus(toStatus);
            reportHandleResume.setUserId(UserUtil.getCurrentUser().getId());
            reportHandleResume.setUserName(UserUtil.getCurrentUser().getName());
            reportHandleResumeDao.create(reportHandleResume);
            map.put("reportHandleResumeId",reportHandleResume.getId());
            if(toStatus.equals(QuarantineCenterHarmlessStatus.FINISHED.value())){

                ProductionReport productionReport = buildProductionReport(quarantineCenterHarmless);
                productionReportDao.create(productionReport);
                BreedFactory breedFactory = new BreedFactory();
                breedFactory.setId(productionReport.getBreedFactoryId());
                breedFactory.setStockQuantity(productionReport.getChangeQuantity());

                breedFactoryDao.updateFromSC(breedFactory);
                if(productionReport.getStableId() != null){
                    BreedFactoryStable breedFactoryStable = new BreedFactoryStable();
                    breedFactoryStable.setId(productionReport.getStableId());
                    breedFactoryStable.setQuantity(productionReport.getChangeQuantity());
                    breedFactoryStableDao.updateFromSC(breedFactoryStable);
                    List<String> earNumbers = quarantineCenterHarmless.getEarNumbers();
                    if(earNumbers != null && !earNumbers.isEmpty()){
                        Map<String , Object> map1 = Maps.newHashMap();
                        map1.put("markNumbers",earNumbers);
                        map1.put("status", AnimalMarkStatus.USED.value());
                        map1.put("outedAt",new Date());
                        animalMarkDao.updateMarks(map1);
                    }
                }
            }

        }else {
            flagQuarantineCenterHarmless = quarantineCenterHarmlessDao.update(quarantineCenterHarmless);
            map.put("flagQuarantineCenterHarmless",flagQuarantineCenterHarmless);
        }
        return map;
    }


    private ProductionReport buildProductionReport(QuarantineCenterHarmless quarantineCenterHarmless) {
        ProductionReport productionReport = new ProductionReport();

        Long stableId = quarantineCenterHarmless.getStableId();
        BreedFactoryStable breedFactoryStable = null;
        Integer breedType = quarantineCenterHarmless.getProductType();
        Integer breedSecondType = 101;
        Integer breedThirdType = 2;
        if(breedType == 1){
            breedSecondType = 101;
        }else if(breedType == 2){
            breedSecondType = 201;
        }else if(breedType == 3){
            breedSecondType = 301;
        }else if (breedType == 4){
            breedSecondType = 0;
            breedThirdType = 0;
        }
        if(stableId != null && breedType != 4){
            breedFactoryStable = breedFactoryStableDao.findById(stableId);
            productionReport.setStableId(quarantineCenterHarmless.getStableId());
            productionReport.setStableNumber(quarantineCenterHarmless.getStableNumber());
            breedSecondType = breedFactoryStable.getBreedSecondType();
            breedThirdType = breedFactoryStable.getBreedThirdType();
        }
        productionReport.setBreedSecondType(breedSecondType);
        productionReport.setBreedThirdType(breedThirdType);
        //判断是否是规模场
        Long breedFactoryId = quarantineCenterHarmless.getBreedFactoryId();
        BreedFactory breedFactory = breedFactoryDao.findById(breedFactoryId);
        if(breedFactory.getIsScale() == 0){
            productionReport.setInQuantity(quarantineCenterHarmless.getQuantity());
            productionReport.setDeathQuantity(quarantineCenterHarmless.getQuantity());
            productionReport.setChangeQuantity(0);
        }else {
            productionReport.setInQuantity(0);
            productionReport.setDeathQuantity(quarantineCenterHarmless.getQuantity());
            productionReport.setChangeQuantity( 0 - quarantineCenterHarmless.getQuantity());
        }
        productionReport.setBreedType(quarantineCenterHarmless.getProductType());

        LoginUser loginUser = UserUtil.getCurrentUser();
        String prefix = "SSC";
        prefix = Utils.createPrefix(prefix,loginUser);//单据编号前缀的开头字母 和 当前用户
        String number = Utils.create(prefix,productionReportDao.findCurrentMaxNumber(prefix));//要在表中拿到当前最大的Number号
        productionReport.setNumber(number);

        productionReport.setRelateId(quarantineCenterHarmless.getId());
        productionReport.setRelateNumber(quarantineCenterHarmless.getNumber());
        productionReport.setInQuantity(0);
        productionReport.setDeathQuantity(quarantineCenterHarmless.getQuantity());
        productionReport.setChangeQuantity( 0 - quarantineCenterHarmless.getQuantity());

        productionReport.setBreedType(quarantineCenterHarmless.getProductType());
        productionReport.setBirthQuantity(0);
        productionReport.setOutQuantity(0);
        productionReport.setBreedFactoryId(quarantineCenterHarmless.getBreedFactoryId());
        productionReport.setBreedFactoryName(quarantineCenterHarmless.getBreedFactoryName());
        productionReport.setDate(new Date());
        productionReport.setUnit(quarantineCenterHarmless.getUnit());


        productionReport.setStatus(ProductionReportStatus.FINISHED.value());
        productionReport.setDeleteFlag(DocumentsDeleteFlag.NORMAL.value());
        return productionReport;
    }
}
